package com.example.commonlib.commonutil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: yuyue
 * Date: 14-7-23
 * Time: 下午7:51
 */
public class JsonHelper {

    /**
     * map转json
     *
     * @param object 是一个map
     * @return
     * @throws JSONException
     */
    public static Object mapToJSON(Object object) throws JSONException {
        if (object instanceof Map) {
            JSONObject json = new JSONObject();
            Map map = (Map) object;
            for (Object key : map.keySet()) {
                json.put(key.toString(), mapToJSON(map.get(key)));
            }
            return json;
        } else if (object instanceof Iterable) {

            JSONArray json = new JSONArray();

            for (Object value : ((Iterable) object)) {
                json.put(value);
            }

            return json;
        } else {
            return object;
        }
    }


    /**
     * list转json
     * @param object
     * @return
     */
    public static JSONArray listToJson(Iterable object){

            JSONArray json = new JSONArray();

            for (Object value : object) {
                json.put(value);
            }

            return json;
    }

    public static boolean isEmptyObject(JSONObject object) {
        return object.names() == null;
    }

    public static Map<String, Object> getMap(JSONObject object, String key) throws JSONException {
        return toMap(object.getJSONObject(key));
    }

    /**
     * json转map
     *
     * @param object
     * @return
     * @throws JSONException
     */
    public static HashMap<String, Object> toMap(JSONObject object) throws JSONException {
        HashMap<String, Object> map = new HashMap();
        Iterator keys = object.keys();
        while (keys.hasNext()) {
            String key = (String) keys.next();
            map.put(key, fromJson(object.get(key)));
        }
        return map;
    }






    /**
     * json转list
     *
     * @param array
     * @return
     * @throws JSONException
     */
    public static List toList(JSONArray array) throws JSONException {
        List list = new ArrayList();
        for (int i = 0; i < array.length(); i++) {
            list.add(fromJson(array.get(i)));
        }
        return list;
    }





    /**
     * 根据情况将json转换为list 或者map
     *
     * @param json
     * @return
     * @throws JSONException
     */
    private static Object fromJson(Object json) throws JSONException {
        if (json == JSONObject.NULL) {
            return null;
        } else if (json instanceof JSONObject) {
            return toMap((JSONObject) json);
        } else if (json instanceof JSONArray) {
            return toList((JSONArray) json);
        } else {
            return json;
        }
    }

    /**
     * 将String转换成json
     *
     * @param s
     * @return
     */
    public static JSONObject stringToJson(String s) {
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(s);

        } catch (JSONException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return jsonObject;
    }

    /**
     * 将String转换成json
     *
     * @param s
     * @return
     */
    public static JSONArray stringToJsonArray(String s) {
        JSONArray jsonObject = null;
        try {
            jsonObject = new JSONArray(s);

        } catch (JSONException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return jsonObject;
    }



}
